//
//  SJZAccountModel.m
//  SJZMetro
//
//  Created by 彭利民 on 2017/6/30.
//  Copyright © 2017年 bwton. All rights reserved.
//

#import "SJZAccountModel.h"

@implementation SJZAccountModel

//归档调用方法
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.userId forKey:@"userId"];
    [aCoder encodeObject:self.phone forKey:@"phone"];
    [aCoder encodeObject:self.token forKey:@"token"];
//    [aCoder encodeObject:self.merchantName forKey:@"merchantName"];

}


//接档调用方法
//取值
- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        
        self.userId =[aDecoder decodeObjectForKey:@"userId"];
        self.phone =[aDecoder decodeObjectForKey:@"phone"];
        self.token =[aDecoder decodeObjectForKey:@"token"];
//        self.merchantName =[aDecoder decodeObjectForKey:@"merchantName"];

    }
    return self;
}


+ (BOOL)saveUserObjectWithUser:(SJZAccountModel *)user
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];
    NSString*filePath=[docPath stringByAppendingPathComponent:@"account.data"];
    //归档操作
    return [NSKeyedArchiver archiveRootObject:user toFile:filePath];
}


+ (SJZAccountModel *)userFromArachiver
{
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];
    NSString*filePath=[docPath stringByAppendingPathComponent:@"account.data"];
    SJZAccountModel *account = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    return account;
}

+ (BOOL)isLogin
{
    if ([SJZAccountModel userFromArachiver].phone) {
        // 有电话  是登录
        return YES;
    }else
    {
        return NO;
    }
}

@end
